import axios, { type AxiosError } from 'axios';
import { message } from 'ant-design-vue';
const instance = axios.create({
  baseURL: '',
  timeout: 60000,
  headers: {
    'Content-Type': 'application/json',
  },
});
// 添加请求拦截器
instance.interceptors.request.use(
  (config) => {
    return config;
  },
  async (error: AxiosError) => {
    await message.error(error.message);
    return Promise.reject(error);
  },
);
// 添加响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response.data;
  },
  async (error: AxiosError) => {
    // 处理错误
    const status = error.response?.status;
    const tipText = (() => {
      switch (status) {
        case 401:
          return '未授权，请登录';
        case 403:
          return '拒绝访问';
        case 404:
          return '请求错误,未找到该资源';
        case 405:
          return '请求方法未允许';
        case 408:
          return '请求超时';
        case 500:
          return '服务器端出错';
        case 501:
          return '网络未实现';
        case 502:
          return '网络错误';
      }
    })();
    await message.error(tipText);
    return Promise.reject(error);
  },
);
export default instance;
